# coding=utf-8
import sys
from function_utils import Global, Common, Human

tar_file = 'oam_collect_sys_logs.tar.gz'
logs = [
    "/var/log/messages*",
    "/var/log/syslog*",
    "/var/log/authlog*",
    "/var/log/boot*",
    "/var/log/kern*",
    "/var/log/mail*",
    "/var/log/cron",
    "/var/log/audit/*",
    "/var/log/secure*",
    "/var/log/lastlog",
    "/var/log/wtmp",
    "/var/log/tallylog"
]


def collect_logs():
    opts = Global.values_parse(sys.argv[2])[1]['Checkbox']
    for i, var in enumerate(opts):
        if not var:
            continue
        print('%s Collecting %s' % (Human.time(), logs[i]))
        ret, out, err = Common.execute_cmd('cp -rf %s %s' % (logs[i], Global.task_path))
        if not ret:
            print(err)

    cmd = 'cd {1} && tar zcvf {0} * && chmod 777 {0}'.format(tar_file, Global.task_path)
    ret, out, err = Common.execute_cmd(cmd)
    if not ret:
        print(err)
    return ret


def write_widgets_value(ret, file):
    Global.values_write([
        {},
        {}
    ])
    if ret:
        Global.report_info(100, download=file)
    else:
        Global.report_err(100)


def main():
    if sys.argv[1] == 'start':
        write_widgets_value(collect_logs(), '{0}/{1}'.format(Global.task_path, tar_file))


if __name__ == '__main__':
    main()
